//
//  STFMainViewController.swift
//  高仿新浪微博
//
//  Created by 沈腾飞 on 2017/1/16.
//  Copyright © 2017年 shen. All rights reserved.
//

import UIKit

class STFMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //实例化自定义tabbar
        let tabbar = STFTabBar()
        
        //kvc 给只读属性赋值
        setValue(tabbar, forKey: "tabBar")
        
        
        //实例化闭包
        tabbar.addButton = {
            
            //回调
            print("点击了添加按钮")
        }
        
        
        
        
//    self.view.backgroundColor = UIColor.white
        
        //添加子控制器
        //首页
        addChildViewController(STFHomeViewController(), title: "首页", imageName: "tabbar_home")
        //消息
        addChildViewController(STFMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        //发现
        addChildViewController(STFDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        //我
        addChildViewController(STFProfileViewController(), title: "我", imageName: "tabbar_profile")
        
        
        
        
        
        
        
        
    }
    
    //MARK: - 添加子控制器的主方法
    
    /// 添加子控制器的主方法
    ///
    /// - Parameters:
    ///   - childController: 控制器
    ///   - title: 名称
    ///   - imageName: 图片名称
    func addChildViewController(_ childController: UIViewController,title : String , imageName : String) {
        //设置title
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        childController.title = title
        //设置文字选中颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: UIControlState.selected)
        //设置图片
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) //设置图片的渲染方式为图片原生颜色
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        //添加子控制器
        addChildViewController(UINavigationController(rootViewController: childController))
        

    }
    
    
    
    
    
    
    
    
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
